/*
 * Copyright (c) 2009 Bogong (www.bogong.dk)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <windows.h>
#include <stdio.h>
#include <celog.h>
#include "..\DumpLogCommon\Common.h"



// Callback which is called from common dumplog code once for each debug message in the log
static void PrintLogLine(
    wchar_t *pszLine,       // Debug message as a text string
    void *pArg              // Unused
    )
{
	wprintf(L"%s\n", pszLine);
}

// Main function
int wmain(int argc, wchar_t *argv[])
{
    HANDLE hFile = INVALID_HANDLE_VALUE;
    wchar_t szInFile[MAX_PATH];

    wprintf(L"Windows CE DumpLog 0.1 (Compile time: %S %S)\n", __DATE__, __TIME__);
    wprintf(L"Copyright (C) 2009 by Bogong (www.bogong.dk)\n\n");

    if (argc > 1)
    {
        wcscpy_s(szInFile, _countof(szInFile), argv[1]);
    }
    else
    {
        HKEY hKey;
        DWORD dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCWSTR)CE_LOG_FILE_PATH, 0, 0, &hKey);
         if (ERROR_SUCCESS == dwResult)
        {
            DWORD dwSize = sizeof(szInFile);
            DWORD dwType = REG_SZ;
            dwResult = RegQueryValueEx(hKey, L"FileName", NULL, &dwType, (LPBYTE)szInFile, &dwSize);
            RegCloseKey(hKey);
            if (dwResult != ERROR_SUCCESS)
            {
                wprintf(L"Failed to read log file name from registry: %d", GetLastError());
                return 1;
            }
        }
        else
        {
            wprintf(L"Failed to open registry key: %d", GetLastError());
            return 1;
        }
    }
    
    hFile = CreateFile(szInFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
    if (hFile != INVALID_HANDLE_VALUE) 
    {
        wprintf(L"Dumping celog file: %s\n", szInFile);
        ProcessLogFile(hFile, PrintLogLine, NULL);
        CloseHandle(hFile);
    }
    else
    {
        wprintf(L"Failed to open file %s: %d\n", szInFile, GetLastError());
        return 1;
    }
    
    return 0;
}
